package com.example.java_second_hand_market;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.geometry.Pos;

import java.sql.*;

public class InfoDialog extends Stage {
    String DBUrl = ParameterDB.ParaDBUserUrl;
    String DBUser = "root", DBPassword = "123456";
    @FXML
    Label iteml = new Label();
    @FXML
    Label pricel = new Label();
    @FXML
    Label sellerl = new Label();
    @FXML
    Label tagl = new Label();
    @FXML
    Button quit = new Button("退出");
    @FXML
    Button buy = new Button("购买");
    Buyer bye;
    Item ite;
    String tag = " ";

    public InfoDialog(Buyer by, Item it, int width, int height) {
        bye = by;
        ite = it;
        iteml.setText(ite.getName());
        pricel.setText(ite.getPrice());
        sellerl.setText(ite.getSeller().getName());

        // Database query to get item tags
        try (Connection con = DriverManager.getConnection(DBUrl, DBUser, DBPassword);
             Statement stmt = con.createStatement()) {

            String query = "SELECT * FROM item_list WHERE id = ?";
            try (PreparedStatement pstmt = con.prepareStatement(query)) {
                pstmt.setInt(1, ite.getInfoId());
                ResultSet rs = pstmt.executeQuery();

                if (rs.next()) {
                    int itemId = rs.getInt("id");

                    String tagQuery = "SELECT t.tag_name FROM tag_item_relation tir JOIN tag_list t ON tir.tag_id = t.id WHERE tir.Item_id = ?";
                    try (PreparedStatement tagPstmt = con.prepareStatement(tagQuery)) {
                        tagPstmt.setInt(1, itemId);
                        ResultSet rs1 = tagPstmt.executeQuery();

                        while (rs1.next()) {
                            String tagName = rs1.getString("tag_name");
                            if (tag.isEmpty()) {
                                tag = tagName;
                            } else {
                                tag += " " + tagName;
                            }
                        }
                        rs1.close();
                    }
                    System.out.println("Tags: " + tag);
                } else {
                    System.out.println("No item found with name: " + ite.getName());
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // Set the tag label
        tagl.setText(tag);

        // Button actions
        quit.setOnAction(actionEvent -> close());
        buy.setOnAction(actionEvent -> {
            PaymentDialog pd = new PaymentDialog(bye, ite, width, height);
        });

        // Main layout
        VBox vb = new VBox(10); // Spacing between elements
        vb.setPadding(new Insets(20)); // Add padding around VBox

        // Top HBox (item info)
        HBox hb1 = new HBox(10); // Spacing between items
        hb1.getChildren().addAll(iteml, pricel, sellerl);
        hb1.setAlignment(Pos.CENTER_LEFT); // Align to left
        hb1.setPadding(new Insets(5, 0, 10, 0)); // Add padding around the HBox

        // Tag label
        tagl.setPadding(new Insets(5, 0, 10, 0)); // Padding for tag label

        // Bottom HBox (buttons)
        HBox hb2 = new HBox(20); // Spacing between buttons
        hb2.setAlignment(Pos.CENTER); // Center the buttons
        hb2.getChildren().addAll(quit, buy);
        hb2.setPadding(new Insets(20, 0, 10, 0)); // Padding around the HBox

        vb.getChildren().addAll(hb1, tagl, hb2);

        // BorderPane layout
        BorderPane bp = new BorderPane();
        bp.setCenter(vb);

        // Set window properties
        setX(820);
        setY(500);
        initModality(Modality.APPLICATION_MODAL);
        setScene(new Scene(bp));
        setWidth(400);
        setHeight(300);
        setX((width - getWidth()) / 2);
        setY((height - getHeight()) / 2);
        setTitle("商品信息");
        show();
    }
}
